package com.lxq.sign.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxq.sign.controller.Code;
import com.lxq.sign.controller.Result;
import com.lxq.sign.dao.LogInfoDao;
import com.lxq.sign.domain.LogInfo;
import com.lxq.sign.domain.StudentInf;
import com.lxq.sign.service.LogInfoService;
import com.lxq.sign.service.StuLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author: KK
 * @date: 2023/11/15 16:22
 * @email: 2551000940@qq.com
 */

@Service
@Slf4j
@SuppressWarnings({"all"})
public class LogInfoServiceImpl implements LogInfoService {

    @Autowired
    StuLoginService stuLoginService;

    @Autowired
    LogInfoDao logInfoDao;

    @Override
    public void saveInfoByStuCode(String stuCode) {
        StudentInf studentInf = stuLoginService.getOneByStuCode(stuCode);
        LocalDateTime now = LocalDateTime.now();

        LogInfo logInfo = new LogInfo();
        logInfo.setCreateTime(now);
        logInfo.setStuName(studentInf == null ? "错误学号" : studentInf.getStuName());
        logInfo.setStuCode(stuCode);
        logInfo.setDayOfWeek(getDayOfWeek());
        logInfoDao.insert(logInfo);
    }

    @Override
    public String getDayOfWeek() {
        LocalDate currentDate = LocalDate.now();
        int year = currentDate.getYear();
        int month = currentDate.getMonthValue();
        int day = currentDate.getDayOfMonth();
        LocalDate date = LocalDate.of(year, month, day);
        String dayOfWeek = date.getDayOfWeek().toString();
        switch (dayOfWeek) {
            case "MONDAY":
                return "周一";
            case "TUESDAY":
                return "周二";
            case "WEDNESDAY":
                return "周三";
            case "THURSDAY":
                return "周四";
            case "FRIDAY":
                return "周五";
            case "SATURDAY":
                return "周六";
            default:
                return "周日";
        }
    }

    @Override
    public Result getLogInfo(Integer currentPage, Integer id) {
        String dayOfWeek = "";
        if (id == 1) {
            dayOfWeek = "周一";
        } else if (id == 2) {
            dayOfWeek = "周二";
        } else if (id == 3) {
            dayOfWeek = "周三";
        } else if (id == 4) {
            dayOfWeek = "周四";
        } else if (id == 5) {
            dayOfWeek = "周五";
        } else {
            dayOfWeek = null;
        }
        LambdaQueryWrapper<LogInfo> queryWrapper = new LambdaQueryWrapper<LogInfo>();
        if (!StringUtils.isEmpty(dayOfWeek)){
            String finalDayOfWeek = dayOfWeek;
            queryWrapper.and(wrapper->{
                wrapper.eq(LogInfo::getDayOfWeek, finalDayOfWeek);
            });
        }
        IPage ipage = new Page(currentPage, 15);
        queryWrapper.orderByAsc(LogInfo::getId);
        IPage iPage = logInfoDao.selectPage(ipage, queryWrapper);
        return new Result(ipage != null ? Code.GET_OK : Code.GET_ERR, ipage);
    }

    @Override
    @Scheduled(cron = "0 0 1 * * 1")
    public void deleteAllLogInfo() {
        //删除所有登录日志
        logInfoDao.delete(null);
    }


}
